import { useHistory, useModel } from 'umi';
import ShowRentCost from '@/components/ShowRentCost';
import { ModelReturn, RentCost } from '@/data-type';
import { memo, useEffect, useState } from 'react';
import { getIdFromHistory, needbackButton } from '@/utils/kits';
const RentCostPage: React.FC = () => {
  // const { dataSource, reload, deleteById, updateOne } = useModel('RentCostModel');
  const model = useModel('RentCostModel');
  // console.log(dataSource);
  // let dataSource: RentCost[] | undefined;
  const reload = () => { return model.query({}) };
  const history = useHistory();
  const [dataSource, setDataSource] = useState<RentCost[]>([]);
  useEffect(() => {
    if (!dataSource || dataSource.length === 0) {
      if (!needbackButton(history.location.state)) {
        model.query({}).then(data => {
          setDataSource(data as RentCost[]);
        })
      } else {
        let id: number = getIdFromHistory(history.location.state) ?? 0;
        model.query({ programId: id })
          .then(data => {
            setDataSource((data as RentCost[]));
          })
      }
    }
  },[])
  const retModel: ModelReturn<RentCost> = {
    ...model,
    dataSource: dataSource,
    reload: reload,
  }
  return (
    <div>
      <a onClick={() => retModel.reload()}>reload</a>
      <ShowRentCost model={retModel} />
    </div>
  );
};

export default RentCostPage;